<?php

namespace App\Http\Controllers\Activity;

use App\App\Services\Web\UserService;
use App\Contracts\Repositories\ActivityRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

trait ExtendBaseController
{
    //活动基础控制器拓展
    protected $user;
    protected $activities;

    public function __construct(UserService $user,ActivityRepository $activities)
    {
        $this->user = $user;
        $this->activities = $activities;
    }
    abstract public function store(Request $request);

    abstract public function show($id);

    abstract public function update();

    abstract protected function parseLimits(Request $request);

    public function checkHttpHeaders(Request $request)
    {
        if (!$request->header('user-id')) {
            throw new AppException('未携带UserId');
        }

        if (!$request->header('group-code')) {
            throw new AppException('未携带GroupCode');
        }
    }
    protected function parseShares(Request $request)
    {
        $shares = [
            'isopen' => $request->json('shares.isopen', 'off'),
            'title' => $request->json('shares.title', ''),
            'content' => $request->json('shares.content', ''),
            'icon' => $request->json('shares.thumb', $request->json('shares.icon', '')),
        ];
        return json_encode($shares);
    }
    protected function formatShares($raw)
    {
        if (empty($raw)) {
            return [
                'isopen' => 'off',
                'title' => '',
                'content' => '',
                'thumb' => '',
                'icon' => '',
            ];
        }

        $shares = json_decode($raw, true);
        $thumb = Arr::get($shares, 'icon', Arr::get($shares, 'thumb', ''));
        $thumb = app(StorageService::class)->toHttpUrl($thumb);

        return [
            'isopen' => Arr::get($shares, 'isopen', 'off'),
            'title' => Arr::get($shares, 'title', ''),
            'content' => Arr::get($shares, 'content', ''),
            'icon' => $thumb,
            'thumb' => $thumb,
        ];
    }

}
